package com.blog.controller.sys.role;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.blog.common.base.BaseController;
import com.blog.common.base.ResponseReslut;
import com.blog.common.util.jstree.Tree;
import com.blog.entity.sys.Menu;
import com.blog.entity.sys.Role;
import com.blog.entity.sys.RoleAndMenu;
import com.blog.entity.sys.UserAndRole;
import com.blog.service.sys.MenuService;
import com.blog.service.sys.RoleAndMenuService;
import com.blog.service.sys.RoleService;
import com.blog.service.sys.UserAndRoleService;
import com.blog.vo.MenuVo;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	
	@Autowired
	private MenuService menuService;
	
	@Autowired
	private RoleAndMenuService roleAndMenuService;
	
	@Autowired
	private UserAndRoleService userAndRoleService;
	
	
	//角色树形弹窗 
	@RequestMapping(value = "/treeView", method = RequestMethod.GET)
	public String treeView() {
		return  "sys/role/deptTree";
	}

	@GetMapping("/tree")
	@ResponseBody
	public Tree<Role> tree() {
		Tree<Role> tree = new Tree<Role>();
		tree = roleService.listRoleTree();
		return tree;
	}
	
	@GetMapping("/list")
	@ResponseBody
	public List<Role> listRoles(@RequestParam Map<String, Object> prams)
	{
		List<Role> list=roleService.listRoles(prams);
		for(Role r : list)
		{
			if(r.getId()==1)  //把 最高权限删除掉   也可以直接在sql语句中排除 
			{
				list.remove(r);
				break;
			}
		}
		return list;
	}
	
	//打开 添加菜单 
	@GetMapping("/add/{pId}")
	public ModelAndView add(@PathVariable("pId") Integer pId) {
		Map<String, Object> map=new HashMap<>();
		map.put("pId", pId);
		if (pId==1) {
			map.put("pName", "根角色");
		} else {
			//如果不是根节点    查询菜单id查询 数据 返回给页面    parentId 其实就是pid 
			 map.put("pName",roleService.selectRoleByRoleId(pId).getName());
		}
		return view("/sys/role/add",map);
	}
	
	
	//保存
	@PostMapping("/save")
	@ResponseBody
	public ResponseReslut saveRole(Role role)
	{
		if(roleService.saveRole(role)>0)
		{
			return setResultSuccess("添加成功");
		}	 
		return resultError("添加失败，请联系管理员");
	}
	
	
	//修改 
	//打开 修改菜单 
	@GetMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable("id") Integer id) {
		Role roleDo=roleService.selectRoleByRoleId(id);
		Integer pId=roleDo.getPid();//父id
		Map<String, Object> map=new HashMap<>();
		if (pId==1) {
			map.put("pName", "根角色");
		} else {
			//如果不是根节点    查询菜单id查询 数据 返回给页面    parentId 其实就是menuId 
			map.put("pName",roleService.selectRoleByRoleId(pId).getName());
		}
		map.put("roleDo", roleDo);
		return view("/sys/role/editRole",map);
	}
	
	
	//保存
	@PostMapping("/update")
	@ResponseBody
	public ResponseReslut updateRole(Role role)
	{
		if(roleService.updateRole(role)>0)
		{
			return setResultSuccess("修改成功");
		}	 
		return resultError("修改失败，请联系管理员");
	}
	
	
	//展示自己用户的权限
	@GetMapping("/qXShow/{roleId}")
	@ResponseBody
	public ResponseReslut qXShow(@PathVariable("roleId") Integer roleId)
	{
		//1.查询出所有的权限 
		List<Menu>  allMenu=menuService.listAllMenu(new HashMap<String,Object>(16));
		//2.得到角色拥有的权限
		List<Menu>  getMenuList=menuService.listMenu(roleId);
		List<Integer> listIds=new ArrayList<>(); //拥有权限的id
		for(Menu m : getMenuList)
		{
			listIds.add(m.getId());
		}
		List<MenuVo> listMuenVo=new ArrayList<>();
		for(Menu  m : allMenu)
		{
			MenuVo mvo=new MenuVo();
			mvo.setId(m.getId());
			for(Integer id : listIds)
			{
				if(m.getId().equals(id))
				{
					mvo.setChecked("true");
					mvo.setOpen("true");
					break;
				}
			}
			mvo.setName(m.getName());
			mvo.setPId(m.getParentId());
			listMuenVo.add(mvo);
		}
		
		return setResultSuccess("查询成功！",listMuenVo); 
	}
	
	
	@PostMapping("saveMenuRole")
	@ResponseBody
	public ResponseReslut  saveMenuRole(String menuIds,Integer roleId)
	{
		//1.先删除关联的中间表信息  根据角色id 
		Integer role_id=roleId;//这里没有修改  
		RoleAndMenu rm=new RoleAndMenu();
		rm.setRole_id(roleId);
		roleAndMenuService.deleteRoleMenu(rm);
		if(StringUtils.isNotEmpty(menuIds))
		{
			String idsStr[]=menuIds.split(",");
			for(int i=0;i<idsStr.length;i++)
			{
				RoleAndMenu roleMenu=new RoleAndMenu();
				//这里需要查询一下数据 看是是否有这个角色
				roleMenu.setRole_id(roleService.selectRoleByRoleId(role_id).getId());
				roleMenu.setMenu_id(menuService.getMenuById(Integer.parseInt(idsStr[i])).getId());
				roleAndMenuService.insertRoleMenu(roleMenu);
			}
		}
		return setResultSuccess("授权成功！"); 
	}
	
	@PostMapping("remove")
	@ResponseBody
	public  ResponseReslut   remove(Integer roleId)
	{
		
		//查询是否有下级，如果有 就不能删除
		Map<String,Object> map=new HashMap<>();
		map.put("pid", roleId);
		List<Role> listRole=roleService.listRoles(map);
		if(listRole.size()>0)
		{
			return resultError("删除失败,请先把子节点删除！");
		}
		
		//1.删除关联的信息  
		UserAndRole ur =new UserAndRole();
		ur.setRole_id(roleId);
		RoleAndMenu rm=new RoleAndMenu();
		rm.setRole_id(roleId);
		userAndRoleService.deleteUserRole(ur);
		roleAndMenuService.deleteRoleMenu(rm);
		roleService.delectRoleById(roleId);
		return setResultSuccess("删除成功！"); 
	}
	
}
